package com.android.slidingmenuexample;

import com.android.slidingmenuexample.layouts.Layout1;
import com.android.slidingmenuexample.menuactivities.EventApplyActivity;
import com.android.slidingmenuexample.menuactivities.MyInfoActivity;
import com.android.slidingmenuexample.menuactivities.NoticeActivity;

import android.content.Intent;
import android.os.*;
import android.support.v4.app.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class SlidingMenuMain extends FragmentActivity {

	MainLayout mLayout;
	private ListView lvMenu;
	private String[] lvMenuItems;
	Button btMenu;
	TextView tvTitle;
	int currentTV;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    
		//getActionBar().setHomeButtonEnabled(true);
		//getActionBar().setIcon(R.drawable.ic_action_select_all);
		
		getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
	    getActionBar().hide();

		mLayout = (MainLayout) this.getLayoutInflater().inflate(
				R.layout.activity_main, null);
		setContentView(mLayout);

		lvMenuItems = getResources().getStringArray(R.array.menu_items);
		lvMenu = (ListView) findViewById(R.id.menu_listview);
		lvMenu.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, lvMenuItems));
		lvMenu.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				onMenuItemClick(parent, view, position, id);
			}

		});

//		btMenu = (Button) findViewById(R.id.button_menu);
//		btMenu.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				// Show/hide the menu
//				toggleMenu(v);
//			}
//		});

		currentTV = 0;
		FragmentManager fm = SlidingMenuMain.this.getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		Layout1 fragment = new Layout1();
		ft.add(R.id.activity_main_content_fragment, fragment);
		ft.commit();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	  super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {

	 
	  if (item.getItemId() == android.R.id.home) {
	    // Show/hide the menu
	    toggleMenu(item);
	  }
	  return false;
	}

	public void toggleMenu(MenuItem item) {
		mLayout.toggleMenu();
	}

	private void onMenuItemClick(AdapterView<?> parent, View view,
	         int position, long id) {

	      Intent intent = null;
	      if (position == 0) {
	    	  intent = new Intent(this, NoticeActivity.class);
	      } else if (position == 1) {
	    	  intent = new Intent(this, MyInfoActivity.class);
	      }
	      else if(position == 2)
	      {
	    	  intent = new Intent(this, EventApplyActivity.class);
	      }

	      if (intent != null) {
	         startActivity(intent);
	         currentTV = position;
	      }
	      mLayout.toggleMenu();
	}

	@Override
	public void onBackPressed() {
		if (mLayout.isMenuShown()) {
			mLayout.toggleMenu();
		} else {
			super.onBackPressed();
		}
	}
}
